package com.kfm.base.io.xml;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.TextNode;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class Demo02 {

    public static void main(String[] args) {
//        String result1= HttpUtil.get("https://m.bqgbi.com/book/1977/24.html");
//        System.out.println(result1);

        outputEbook("https://m.bqgbi.com/book/764/1.html", "完美世界");

    }

    public static void outputEbook(String url, String name){
        // 发送请求
        HttpResponse response = HttpRequest.get(url)
                .execute();

        // 网页响应的结果
        String string = response.toString();

        // Jsoup 解析 html
        Document parse = Jsoup.parse(string);

        // 根据元素的 id 查找元素
        Element chaptercontent = parse.getElementById("chaptercontent");

        // 拿到所有的文本节点
        List<TextNode> textNodes = chaptercontent.textNodes();
//        List<Node> nodes = chaptercontent.childNodes();

        // 写到文件中
        try (FileOutputStream fos = new FileOutputStream("d://io/" + name + ".txt", true);){

            for (TextNode node : textNodes){
                // 过滤广告
                if (!node.toString().contains("bqgbi")){
                    fos.write(node.toString().getBytes());
                }
            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 找到下一章的链接
        String attr = parse.getElementById("pb_next").attr("href");

        if (attr.contains("11")){ // 11 是最后一章
            return;
        }

        final String pre_path = "https://m.bqgbi.com";

        // 下一章的链接
        String nextUrl = pre_path + attr;

        // 休息 1 秒
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        // 递归调用
        outputEbook(nextUrl, name);
    }
}
